import { register, getVercode } from '../../../../../common/api/index'
Page({
    data: {
		checked: false,//勾选
		nickname: null,
		mobile: null,
		password: null,
		comfirmPwd: null,
		uuid: null,
		xcxCode: null,
		headImg: null,
		salt: null,
		code: null,
		imgPath: null,
	},
	// 带邀请码进入注册页
    onLoad(options) {
		if(options.salt){
			this.setData({
				salt: options.salt
			})
		}
	},
	onShow(){
		// 自动获取验证码
		this.getVercodeFunc()
	},
	// 获取验证码
	getVercodeFunc(){
		getVercode().then(res => {
			if(res.code == 200){
				let imgPath = res.data.img.replace(/[\r\n]/g,"");
				this.setData({
					imgPath: imgPath,
					uuid:res.data.uuid
				})
			}
		})
	},
	// 输入变化
	onChange(e){
		let key = e.target.dataset.name;
		this.setData({
			[key]: e.detail
		})
	},
	// 勾选隐私政策
	checkBoxChange(){
		let bool = !this.data.checked
		this.setData({
			checked: bool
		})
	},
	// 去登陆
	toLogin(){
		wx.reLaunch({
		  url: '/pages/component/mine/account/login/index',
		})
	},
	// 点击用户协议
	userAgreement(){
		wx.navigateTo({
		  url: '/pages/component/mine/userAgreement/index/index',
		})
	},
	// 隐私政策
	userAgreementPrivate(){
		wx.navigateTo({
		  url: '/pages/component/mine/userAgreement/private/index',
		})
	},

	toRegister(){
		let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/;
		if(!this.data.nickname){
			wx.showToast({
				title: '请正确输入昵称',
				icon: 'none',
				duration: 2000
			})
			return
		}
		if(!this.data.mobile){
			wx.showToast({
				title: '请正确输入手机号',
				icon: 'none',
				duration: 2000
			})
			return
		}
		if(!this.data.password || !reg.test(this.data.password)){
			wx.showToast({
				title: '请正确输入密码',
				icon: 'none',
				duration: 2000
			})
			return	
		}
		if(this.data.comfirmPwd != this.data.password){
			wx.showToast({
				title: '两次输入密码不一致',
				icon: 'none',
				duration: 2000
			})
			return
		}
		if(!this.data.code){
			wx.showToast({
				title: '请正确输入验证码',
				icon: 'none',
				duration: 2000
			})
			return
		}
		if(!this.data.checked){
			wx.showToast({
				title: '请先阅读并同意用户协议和隐私政策',
				icon: 'none',
				duration: 2000
			})
			return
		}
		wx.login({
		  success: (res) => {
			this.setData({
				xcxCode : res.code
			})
		  },
		})
		wx.getUserProfile({
            desc: '展示用户信息',
            success: (res) => {
				console.log(res,'=============================')
				this.setData({
					headImg : res.userInfo.avatarUrl
				})
				let data = {
					nickname:  this.data.nickname,
					mobile:  this.data.mobile,
					password:  this.data.password,
					headImg:  this.data.headImg,
					code:  this.data.code,
					uuid:  this.data.uuid,
					xcxCode:  this.data.xcxCode,
					salt:  this.data.salt
				}
				register(data).then(res => {
					if(res.code == 200){
						wx.showToast({
							title: '注册成功',
							icon: 'none',
							duration: 2000
						})
						setTimeout(() => {
							wx.reLaunch({
							  url: '/pages/component/mine/account/login/index',
							})
						},2000)
					}
				})
            }
		})
	},
	// 自动获取微信手机号
	getPhoneNumber (e) {
		console.log(e.detail.code)  // 动态令牌
		console.log(e.detail.errMsg) // 回调信息（成功失败都会返回）
		console.log(e.detail.errno)  // 错误码（失败时返回）
	},








    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})